//#define __DEBUG__
#define THIS_FILE "fn_updatemarkers.sqf"
#include "x_setup.sqf"

KEGs_markersrun = true;
private ["_markers", "_disp", "_cMapFull", "_mapFull", "_mappos", "_markedVehicles", "_i", "_m", "_u", "_OriginalSide", "_type", "_icon"];
_markers = _this select 0;
disableSerialization;
_disp = findDisplay 55001;
_cMapFull = 55014;
if (ctrlVisible _cMapFull) then {
	_mapFull = _disp displayctrl _cMapFull;
	_mappos = _mapFull posScreenToWorld[0.5, 0.5];
	KEGs_cam_fullmap camSetTarget _mappos;
	KEGs_cam_fullmap camSetRelPos [0, -1, 150];
	KEGs_cam_fullmap camCommit 0;
};

_markedVehicles = [];
for "_i" from 0 to (count _markers - 1) do {
	if (KEGs_exitspect) exitWith {};
	if (_i >= count _markers) exitWith {};
	_mo = _markers select _i;
	_m = _mo select 0;
	_u = _mo select 1;
	if (KEGs_MissileCamActive) then {
		waitUntil {!KEGs_MissileCamActive || {KEGs_exitspect}};
	};
	if (_u in KEGs_units) then {
		if (speed vehicle _u > 0) then {
			_m setMarkerPosLocal (visiblePositionASL (vehicle _u));
		} else {
			if (alive _u) then {
				_hpos = getPosASL (vehicle _u); _mpos = markerPos _m;
				if (_hpos select 0 != _mpos select 0 || {_hpos select 1 != _mpos select 1}) then {
					_m setMarkerPosLocal (visiblePositionASL  (vehicle _u));
				};
			};
		};
		
		_OriginalSide = _u call KEGs_fnc_CheckOriginalSide;
		if (!(_OriginalSide in KEGs_ShownSides)) then {
			if (markerAlpha _m != 0) then {_m setMarkerAlphaLocal 0};
		} else { 				
			if (markerAlpha _m == 0) then {_m setMarkerAlphaLocal 1};
			if (KEGs_MarkerNames || {KEGs_MinimapZoom < 0.15}) then {
				if (ctrlVisible _cMapFull) then {
					switch(KEGs_MarkerType) do {
						case 0: {
							if (markerText _m != "") then {_m setMarkerTextLocal ""};
						};
						case 1: {
							if (alive (vehicle _u) && {markerText _m != (_mo select 2)}) then {_m setMarkerTextLocal (_mo select 2)};
						};
						case 2: {
							_na = getText (configFile/"CfgVehicles"/typeOf (vehicle _u)/"DisplayName");
							if (markerText _m != _na) then {_m setMarkerTextLocal _na};
						};
					};
				} else {
					if (markerText _m != "") then {_m setMarkerTextLocal ""};
				};
				
				_icon = (vehicle _u) getVariable "KEGs_icon";
				if (isNil "_icon") then {
					_icon = switch (getText(configFile/"CfgVehicles"/typeOf (vehicle _u)/"simulation")) do {
						case "tank": {"n_armor"};
						case "motorcycle";
						case "car": {"n_motor_inf"};
						case "soldier": {"n_inf"};
						case "airplane": {"n_plane"};
						case "helicopter": {"n_air"};
						default {"Arrow"};
					};
					(vehicle _u) setVariable ["KEGs_icon", _icon];
				};
				if (markerType _m != _icon) then {_m setMarkerTypeLocal _icon};
				_siz = markerSize _m;
				_nnsiz = 0.42 * KEGs_MarkerSize;
				if (_siz select 0 != _nnsiz || {_siz select 1 != _nnsiz}) then {_m setMarkerSizeLocal [_nnsiz, _nnsiz]};
				_ddir = direction vehicle _u;
				if (markerDir _m != _ddir) then {_m setMarkerDirLocal _ddir};
			} else {
				if (markerText _m != "") then {_m setMarkerTextLocal ""};
				if (markerType _m != "hd_dot") then {_m setMarkerTypeLocal "hd_dot"};
				_siz = markerSize _m;
				if (_siz select 0 != 0.4 || {_siz select 1 != 0.4}) then {_m setMarkerSizeLocal [0.4, 0.4]};
			};
		};
		
		if (!alive _u) then {_m setMarkerColorLocal "ColorBlack"};
		
		if (vehicle _u in _markedVehicles) then {
			if (markerAlpha _m != 0) then {_m setMarkerAlphaLocal 0};
		} else {
			_markedVehicles set [count _markedVehicles, vehicle _u];
		};
		sleep 0.01;
	} else {
		if (markerAlpha _m != 0) then {_m setMarkerAlphaLocal 0};
		sleep 0.01;
	};
};
KEGs_markersrun = false;